<?php

namespace App\Repositories;

use App\Models\SystemConfig;
use Illuminate\Support\Facades\Cache;

class SystemConfigRepository
{
    public function getValue($key, $default = null)
    {
        return SystemConfig::getValue($key, $default);
    }

    public function getGroup($group): array
    {
        $cacheKey = 'system_config:group:' . $group;
        return Cache::remember($cacheKey, 60*24, function () use ($group) {
            return SystemConfig::query()->where('group', $group)->pluck('value', 'key')->toArray();
        });
    }

    public function updateGroup($group, array $data): void
    {
       foreach ($data as $key => $value) {
           SystemConfig::query()->where('key', $key)->update(['value' => $value]);
           Cache::forget('system_config:key:' . $key);
       }
       Cache::forget('system_config:group:' . $group);
    }
}
